package com.jtest.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jtest.entity.UserEntity;
import com.jtest.mapper.UserMapper;
import com.jtest.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {


    private UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void addUser(UserEntity user) {
        user.setId(null);
        save(user);
    }

    @Override
    public void updateUser(Long id, String name, int age) throws Exception {
        UserEntity user = getUser(id);
        if(null != user){
            user.setName(name);
            user.setAge(age);
            updateById(user);
        }else{
            throw new Exception("记录不存在");
        }
    }

    @Override
    public void deleteUser(Long id) throws Exception {
        UserEntity user = getUser(id);
        if(null != user){
            this.removeById(id);
        }else{
            throw new Exception("记录不存在");
        }
    }

    @Override
    public UserEntity getUser(Long id) {
        return getById(id);
    }
}
